#include "mypushbutton.h"
#include<QDebug>
#include<QPropertyAnimation>

myPushButton::myPushButton(QString normalImg,QString pressImg){
    //自己封装控件
    this->normalImgPath=normalImg;
    this->pressImgPath=pressImg;

    QPixmap pix;
    bool ret=pix.load(normalImg);
    //判断图片是否加载成功
    if(!ret){
        qDebug()<<"图片加载失败";
        return;
    }
    //设置图片固定大小
    this->setFixedSize(pix.width(),pix.height());

    //设置不规则图片样式
    this->setStyleSheet("QPushButton{border:0px}");

    //设置图标
    this->setIcon(pix);
    //设置图标大小
    this->setIconSize(QSize(pix.width(),pix.height()));
}

void myPushButton::zoom1(){
    //创建动画对象
    QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
    //设置动画时间间隔
    animation->setDuration(200);

    //起始位置
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //结束位置
    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    //执行动画
    animation->start();


}
void myPushButton::zoom2(){
    //创建动画对象
    QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
    //设置动画时间间隔
    animation->setDuration(250);

    //起始位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //结束位置
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);

    //执行动画
    animation->start();
}

void myPushButton::mousePressEvent(QMouseEvent *e){
    if(this->pressImgPath!=""){//传入的按下图片不为空即切图
        QPixmap pix;
        bool ret=pix.load(pressImgPath);
        //判断图片是否加载成功
        if(!ret){
            qDebug()<<"图片加载失败";
            return;
        }
        //设置图片固定大小
        this->setFixedSize(pix.width(),pix.height());

        //设置不规则图片样式
        this->setStyleSheet("QPushButton{border:0px}");

        //设置图标
        this->setIcon(pix);
        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));
    }

    //其他的!!让父类执行
    return QPushButton::mousePressEvent(e);
}
void myPushButton::mouseReleaseEvent(QMouseEvent *e){
    if(this->pressImgPath!=""){//传入的按下图片不为空，有切过，切回去
        QPixmap pix;
        //切回初始图片
        bool ret=pix.load(normalImgPath);


        if(!ret){
            qDebug()<<"图片加载失败";
            return;
        }

        this->setFixedSize(pix.width(),pix.height());

        this->setStyleSheet("QPushButton{border:0px}");

        this->setIcon(pix);

        this->setIconSize(QSize(pix.width(),pix.height()));
    }

    //其他的!!让父类执行
    return QPushButton::mouseReleaseEvent(e);
}

